package com.kabigon.handler;

import com.kabigon.message.SendResponse;
import com.kabigon.message.SendToAllRequest;
import com.kabigon.message.SendToUserReqeust;
import com.kabigon.utils.WebSocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.handler
 * @data 2023/3/10 2:54
 */
@Component
public class SendToAllHandler implements MessageHandler<SendToAllRequest> {
    @Override
    public void execute(Session session, SendToAllRequest message) {
        //  这里，先假设直接成功
        SendResponse sendResponse = new SendResponse();
        sendResponse.setCode(0);
        sendResponse.setMsgId(message.getMsgId());
        WebSocketUtil.send(session, SendResponse.TYPE, sendResponse);

    //    创建转发消息
        SendToUserReqeust sendToUserReqeust = new SendToUserReqeust();
        sendToUserReqeust.setMsgId(message.getMsgId());
        sendToUserReqeust.setContent(message.getContent());
    //    广播发送
        WebSocketUtil.broadcast(SendToAllRequest.TYPE, sendToUserReqeust);
    }

    @Override
    public String getType() {
        return SendToAllRequest.TYPE;
    }
}
